<?php

namespace App\Http\Controllers\Order;

use App\Func\Customer\CustomerFunc;
use App\Func\Order\OrderFunc;
use App\Func\User\UserFunc;
use App\Http\Controllers\BasicInterfaceController;
use App\Http\Requests\Order\OrderRequest;
use App\Http\Requests\System\ListRequest;
use App\Http\Service\Order\UserOrderService;

class UserOrderController implements BasicInterfaceController
{

    public function getList()
    {
        $request = request()->only('page','page_size','filters','desc');
        (new ListRequest())->check($request);
        (new UserOrderService())->getList($request);
    }

    public function getSelect()
    {
    }

    public function getSelectHasPage()
    {
    }

    public function getInfo($id)
    {
    }

    /**
     * 稍微特殊了一点的 create
     * @return void
     */
    public function create()
    {
        $payData = request()->only(
            'pay_info'
        );
        // TODO 这里用的管理员登录做测试
        // $customer = (new CustomerFunc())->getLoginInfo(canAll: true);
        $customer = (new UserFunc())->getLoginInfo(canAll: true);
        // 获取用户公钥 并且解密
        // 解密支付参数 通过用户公钥直接对整个JSON字符串进行加密
        $rsaData = rsa_decode($customer['pv_key'],$payData['pay_info']);
        if(!$rsaData){
            s_fail_return(null,trans('message.order_err',[
                'name' => 1001
            ]));
        }
        $data = json_decode($rsaData,true);

        (new OrderRequest())->scene('create')->check($data);
        $orderFunc = new OrderFunc();
        $config = $orderFunc->getPayConfigBySW($data['sw'] ?? 1);
        (new OrderFunc())->createOrder($data['goods_info'],$data['address_id'],$config,$data['prefix'],$data['pay_type']);
        s_c_return(null,trans('message.order_ok'));
    }

    public function fakeDelete($id)
    {

    }

    public function fakeDeleteByIds($ids)
    {
        $data = request()->only('order_ids');
        (new OrderRequest())->scene('delete')->check($data);
        (new UserOrderService())->fakeDeleteByIds($data['order_ids']);
    }



    public function delete()
    {

    }

    public function update($id)
    {

    }
}
